import random

import pygame

from Action.Walk import WalkDir, WalkSoldier


class JapaneseSoldier(WalkSoldier):
    def __init__(self, x, y):
        self.dir_count = 5
        soldier_id = random.randint(1, 4)
        work_path = "./Resources/images/japanese/soldier{0}.png".format(soldier_id)
        super(JapaneseSoldier, self).__init__(work_path, x, y, 100)
        self.count = 5
        self.key = pygame.K_DOWN

    def run(self, down_flag, key_list, obstacle_group):
        if self.dir_count == 0:
            self.dir_count = 20
            key = random.randint(0, 2)
            if key == 0:
                self.walk_action.set_dir(WalkDir.left)
            elif key == 1:
                self.walk_action.set_dir(WalkDir.right)
            elif key == 2:
                self.walk_action.set_dir(WalkDir.up)
            elif key == 3:
                self.walk_action.set_dir(WalkDir.down)
        else:
            self.dir_count -= 1
        off_x = 0
        off_y = 0
        if self.walk_action.walk_dir == WalkDir.down:
            off_y += 10
        elif self.walk_action.walk_dir == WalkDir.up:
            off_y += -10
        elif self.walk_action.walk_dir == WalkDir.left:
            off_x += -10
        elif self.walk_action.walk_dir == WalkDir.right:
            off_x += 10
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        obstacle_list = pygame.sprite.spritecollide(self, obstacle_group, False)
        if len(obstacle_list) > 0:
            self.rect = temp_rect
        else:
            self.pos_x += off_x
            self.pos_y += off_y

    def stand(self, down_flag, key_list, obstacle_group):
        pass
